<?php

// +----------------------------------------------------------------------

// | Fanwe 多语商城建站系统 (Build on ThinkPHP)

// +----------------------------------------------------------------------

// | Copyright (c) 2009 http://www.fanwe.com All rights reserved.

// +----------------------------------------------------------------------

// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )

// +----------------------------------------------------------------------

// | Author: 云淡风轻(97139915@qq.com)

// +----------------------------------------------------------------------



// 后台首页

class IndexAction extends CommonAction{

	public function index()

	{

		$this->display();

	}

	

// 顶部页面

	public function top() {

		C('SHOW_RUN_TIME',false);			// 运行时间显示

		C('SHOW_PAGE_TRACE',false);

		$model	=	M("RoleNav");

		if($_SESSION[C("ADMIN_AUTH_KEY")])

			$list	=	$model->field('id,name')->order("sort")->findAll(); 

		else

			$list	=	$model->where('status=1')->field('id,name')->order("sort")->findAll(); 

		$this->assign('roleNav',$list);

		$this->display();

	}

	// 尾部页面

	public function footer() {

		C('SHOW_RUN_TIME',false);			// 运行时间显示

		C('SHOW_PAGE_TRACE',false);

		$this->display();

	}

	// 菜单页面

	public function menu() {

		

        if(isset($_SESSION[C('USER_AUTH_KEY')])) {

            //显示菜单项

            $id	=	intval($_REQUEST['tag'])==0?2:intval($_REQUEST['tag']);



            $menu  = array();

            if($id>0)

            {

//	            if(isset($_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')].$id])) {

//	

//	                //如果已经缓存，直接读取缓存

//	                $menu   =  $_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')].$id];

//	            }else {

	                //读取数据库模块列表生成菜单项

	                $node_model    =   M("RoleNode");				

					$where['status']=1;

					$where['nav_id']=$id;

	                $list	=	$node_model->where($where)->field('id,action,action_name,module,module_name')->order('sort asc')->select();

	                foreach($list as $key=>$action) {

	                   $menu[$action['module']]['navs'][] = $action;

	                   $menu[$action['module']]['name']	= $action['module_name'];

	                }

	                //缓存菜单访问

	                //$_SESSION['menu'.$_SESSION[C('USER_AUTH_KEY')].$id]	=	$menu;

//	            }

            }

            $this->assign('menu',$menu);

		}

		C('SHOW_RUN_TIME',false);			// 运行时间显示

		C('SHOW_PAGE_TRACE',false);

		$this->display();

	}



	public function menumain() {

		

        if(isset($_SESSION[C('USER_AUTH_KEY')])) {

            //显示菜单项

            $id	=	intval($_REQUEST['tag'])==0?1:intval($_REQUEST['tag']);



            

					$node_model    =   M("RoleNode");				

					$where['status']=1;

					$where['nav_id']=$id;

	                $main_page	=	$node_model->where($where)->field('id,action,action_name,module,module_name')->order('sort asc')->find();

	         C('SHOW_RUN_TIME',false);			// 运行时间显示

			 C('SHOW_PAGE_TRACE',false);

			 $this->redirect(U($main_page['module']."/".$main_page['action']));

		}



	}

	

    // 后台首页 查看系统信息

    public function main()

	{
		$ruleVersion = D("SysConf")->where("name = 'SITE_RULE_VERSION'")->getField("val");

        $info = array(

            '系统名称'=>SHOP_NAME."——团购导航系统",

            L('SYS_VERSION')=>fanweC('SYS_VERSION')."&nbsp;&nbsp;<span id='version_tip'></span>&nbsp;&nbsp;<span id='public_tip'></span>",

            L('SERVER_TIME')=>toDate(gmtTime(),"Y年n月j日 H:i:s"),

            L('BJ_TIME')=>date("Y".L('YEAR')."n".L('MONTH')."j".L('DAY')." H:i:s",gmtTime()+ (8*3600)),

        );

        $this->assign('info',$info);

		$this->assign('ruleVersion',floatval($ruleVersion));

        $this->display();

    }

    

// 更换密码

    public function changePwd()

    {

        //对表单提交处理进行处理或者增加非表单数据

		if(md5($_POST['verify'])	!= $_SESSION['verify']) {

			$this->error(L('VERIFY_ERROR'));

		}

		$map	=	array();

        $map['adm_pwd']= pwdHash($_POST['oldpassword']);

        if(isset($_POST['adm_name'])) {

            $map['adm_name']	 =	 $_POST['adm_name'];

        }elseif(isset($_SESSION[C('USER_AUTH_KEY')])) {

            $map['id']		=	$_SESSION[C('USER_AUTH_KEY')];

        }

        //检查用户

        $User    =   new AdminModel();

        $result = $User->create();

        if (!$result){		

		// 如果创建失败 表示验证没有通过 输出错误提示信息

			$this->error($User->getError());

		}

        

        if(!$User->where($map)->field('id')->find()) {

            $this->error(L('PWD_OR_NAME_ERROR'));

        }else {

			$User->adm_pwd	=	pwdHash($_POST['adm_pwd']);

			$User->save();

			$this->success(L('CHANGE_PWD_SUCCESS'));

         }

    }

	

	public function getRuleVersion()

	{

		$client = $this->phprpcInit();

		$version = $client->getVersion();

		echo $version;

	}

}

?>